﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ClickToCall.ClickToCallToolbar
{
    public partial class FormSettings : ClickToCall.CustomControls.Form
    {
        private string _disabledMessage = 
        @"This feature has been disabled because the {0} installation has not been detected.";
        private bool _disabledOutlook = false;
        private bool _disabledCommunicator = false;
        private Settings _settings;

        RegistrySettingsProvider _regProvider = new RegistrySettingsProvider();
        public FormSettings()
        {
            InitializeComponent();
            _settings = _regProvider.Load();
        }

        public FormSettings(bool isOutlook, bool isCommunicator)
            : this()
        {
            _disabledCommunicator = (isCommunicator) ? false : true;
            _disabledOutlook = (isOutlook) ? false : true;
        }

        bool _searchOutlook = false;
        public bool SearchOutlook
        {
            get
            {
                return _searchOutlook;
            }
            set { _searchOutlook = value; }
        }

        bool _searchCommunicator = false;
        public bool SearchCommunicator
        {
            get
            {
                return _searchCommunicator;
            }
            set { _searchCommunicator = value; }
        }

        bool _isChanged = false;
        public bool ChangeMade
        {
            get
            {
                return _isChanged;
            }
            set { _isChanged = value; }
        }

        private void listBoxLocations_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (panelDisabled.Visible) panelDisabled.Visible = false;

            if (this.listBoxLocations.SelectedIndex == 0)// OC
            {
                if (_disabledCommunicator)
                {
                    SetDisabledMessage("Microsoft Office Communicator 2007");
                    return;
                }
                this.panelOutlook.Visible = false;
                this.panelCommunicator.Visible = true;
                checkBoxSearch.Checked = _searchCommunicator;
            }
            else if (this.listBoxLocations.SelectedIndex == 1)//Outlook
            {
                if (_disabledOutlook)
                {
                    SetDisabledMessage("Microsoft Outlook 2007");
                    return;
                }
                this.panelCommunicator.Visible = false;
                this.panelOutlook.Visible = true;
                checkBoxSearch.Checked = _searchOutlook;
            }
        }

        private void SetDisabledMessage(string p)
        {
            panelDisabled.Dock = DockStyle.Fill;
            panelDisabled.BringToFront();
            panelDisabled.Visible = true;
            labelDisabledMessage.Text = string.Format(_disabledMessage, p);
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void checkBoxSearch_CheckedChanged(object sender, EventArgs e)
        {
            if (this.listBoxLocations.SelectedIndex == 0)// OC
            {
                _searchCommunicator = checkBoxSearch.Checked;
            }
            else if (this.listBoxLocations.SelectedIndex == 1)//Outlook
            {
                _searchOutlook = checkBoxSearch.Checked;
            }
        }

        private void FormSettings_Load(object sender, EventArgs e)
        {
            this.SearchCommunicator = _settings.SearchOC;
            this.SearchOutlook = _settings.SearchOutlook;
            this.checkBoxOCHome.Checked = _settings.UseOCHome;
            this.checkBoxOCMobile.Checked = _settings.UseOCMobile;
            this.checkBoxOCWork.Checked = _settings.UseOCWork;
            this.checkBoxOlkCompany.Checked = _settings.UseOlkCompany;
            this.checkBoxOlkHome.Checked = _settings.UseOlkHome;
            this.checkBoxOlkMobile.Checked = _settings.UseOlkMobile;
            this.checkBoxOlkPrimary.Checked = _settings.UseOlkPrimary;
            this.checkBoxOlkWork.Checked = _settings.UseOlkWork;
            this.checkBoxOlkWork2.Checked = _settings.UseOlkWork2;

            this.listBoxLocations.SelectedIndex = 0;
        }

        private void FormSettings_FormClosed(object sender, FormClosedEventArgs e)
        {
            panelDisabled.Visible = false;
            if (this.DialogResult == DialogResult.OK)
            {
                _settings.SearchOC = this.SearchCommunicator;
                _settings.SearchOutlook = this.SearchOutlook;
                _settings.UseOCHome = this.checkBoxOCHome.Checked;
                _settings.UseOCMobile = this.checkBoxOCMobile.Checked;
                _settings.UseOCWork = this.checkBoxOCWork.Checked;
                _settings.UseOlkCompany = this.checkBoxOlkCompany.Checked;
                _settings.UseOlkHome = this.checkBoxOlkHome.Checked;
                _settings.UseOlkMobile = this.checkBoxOlkMobile.Checked;
                _settings.UseOlkPrimary = this.checkBoxOlkPrimary.Checked;
                _settings.UseOlkWork = this.checkBoxOlkWork.Checked;
                _settings.UseOlkWork2 = this.checkBoxOlkWork2.Checked;
                try
                {
                    _regProvider.Save(_settings);
                }
                catch { }
            }
        }

        private void checkBox_Click(object sender, EventArgs e)
        {
            ChangeMade = true;
        }
    }
}
